"""
MongoDB | 模型
"""


from models import AnyTypeBase
from utils.data_utils.special import ObjectIdStr
from utils.validator_utils.pydantic_validator.field import (
    CharField,
    IntegerField,
    ListField,
    NormalField,
)


class _WriteResult(AnyTypeBase):
    """ 数据写入 结果 """

    acknowledged: bool = NormalField(True, title="数据写入是否成功")


class InsertOneResult(_WriteResult):
    """
    插入 | 单条数据 | 结果 | Model

    - acknowledged (bool): 数据写入是否成功
    - inserted_id (ObjectIdStr): ObjectId
    """

    inserted_id: ObjectIdStr = CharField(..., title="ObjectId")


class InsertManyResult(_WriteResult):
    """
    插入 | 多条数据 | 结果 | Model

    - acknowledged (bool): 数据写入是否成功
    - inserted_ids (list[ObjectIdStr]): ObjectId 列表
    """

    inserted_ids: list[ObjectIdStr] = ListField(..., title="ObjectId 列表", min_length=0)


class UpdateResult(_WriteResult):
    """
    更新数据 | 结果 | Model

    - acknowledged (bool): 数据写入是否成功
    - matched_count (int): 匹配到的文档数
    - modified_count (int): 更新的文档数
    - upserted_id (ObjectIdStr | None): 更新插入的 ObjectId
    """

    matched_count: int = IntegerField(..., title="匹配到的文档数", min_value=0)
    modified_count: int = IntegerField(..., title="更新的文档数", min_value=0)
    upserted_id: ObjectIdStr | None = NormalField(None, title="更新插入的 ObjectId")  # noqa: ignore[spell]


class DeleteResult(_WriteResult):
    """
    删除数据 | 结果 | Model

    - acknowledged (bool): 数据写入是否成功
    - deleted_count (int): 删除的文档数
    """

    deleted_count: int = IntegerField(..., title="删除的文档数", min_value=0)


class BulkWriteResult(_WriteResult):
    """
    批量写入数据 | 结果 | Model

    - acknowledged (bool): 数据写入是否成功
    - inserted_count (int): 插入的文档数
    - matched_count (int): 匹配到的文档数
    - modified_count (int): 更新的文档数
    - deleted_count (int): 删除的文档数
    - upserted_count (int): 更新插入的文档数
    - upserted_ids (dict[int, ObjectIdStr | None]): 更新插入的 ObjectId 列表
    """

    deleted_count: int = IntegerField(..., title="删除的文档数", min_value=0)
    inserted_count: int = IntegerField(..., title="插入的文档数", min_value=0)
    matched_count: int = IntegerField(..., title="匹配到的文档数", min_value=0)
    modified_count: int = IntegerField(..., title="更新的文档数", min_value=0)
    upserted_count: int = IntegerField(..., title="更新插入的文档数", min_value=0)  # noqa: ignore[spell]
    upserted_ids: dict[int, ObjectIdStr | None] = NormalField(..., title="索引-ObjectId 映射")  # noqa: ignore[spell]
